package com.yunhoo.contract.bean;

import com.yunhoo.contract.db.impl.ContractImpl;
import com.yunhoo.contract.model.MainInformation;
import com.yunhoo.contract.util.DocumentQueryConditionBeanContract;
import com.yunhoo.sys.util.common.FacesUtils;
import com.yunhoo.sys.util.common.NavigationTool;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean(name = "contractListBean")
@SessionScoped
public class ContractListBean implements Serializable {

    private DataModel<MainInformation> contractModel = null;
    DocumentQueryConditionBeanContract queryCondition = new DocumentQueryConditionBeanContract();//动态查询条件
    private boolean querybut = true;

    public ContractListBean() {
        contractModel = new ListDataModel<MainInformation>();
    }

    //合同 报表部分
    private void initContractli() {
        List<MainInformation> mainContractList = ContractImpl.queryContractList(null);
        contractModel.setWrappedData(mainContractList);
    }

    public void execSelected() {//合同 明细查看
        String number = contractModel.getRowData().getNumber();

        int type = Integer.parseInt(number.substring(0, 1));

        ContractInitBean contractIntiBean = (ContractInitBean) FacesUtils.getManagedBean("contractInitBean");
        contractIntiBean.setType(type);
        contractIntiBean.toggle();

        if (type == 0) {
            ContractBean contractBean = (ContractBean) FacesUtils.getManagedBean("contractBean");
            contractBean.init(number);
        } else {
            SupplyContractBean supplyContractBean = (SupplyContractBean) FacesUtils.getManagedBean("supplyContractBean");
            supplyContractBean.init(number);
        }

        NavigationTool.getSelectedUrl("/page/contract/business/contract.xhtml");
    }

    public void query() {//合同报表动态查询
        List<MainInformation> mainContractList = ContractImpl.queryContractList(conditionMap());
        contractModel.setWrappedData(mainContractList);
    }

    public void queryCancel() {
        initContractli();
        queryCondition = new DocumentQueryConditionBeanContract();
    }

    public void hiddenbut() {
        querybut = false;
    }

    public void showbut() {
        querybut = true;
    }

    public void goback() {
        NavigationTool.getSelectedUrl("/page/contract/list/contractlist.xhtml");        
    }

    private Map conditionMap() {
        Map params = new HashMap();
        params.put("number", queryCondition.getNumber());
        params.put("name", queryCondition.getContractType().getName());
        params.put("cname", queryCondition.getCname());
        params.put("code", queryCondition.getContractType().getCode());
        params.put("orderdatestart", queryCondition.getOrderdatestart());
        params.put("orderdateend", queryCondition.getOrderdateend());
        params.put("address", queryCondition.getAddress());
        params.put("startstart", queryCondition.getStartstart());
        params.put("startend", queryCondition.getStartend());
        params.put("deadlinestart", queryCondition.getDeadlinestart());
        params.put("deadlineend", queryCondition.getDeadlineend());
        params.put("nameA", queryCondition.getNameA());
        params.put("nameAstand", queryCondition.getNameAstand());
        params.put("partnerName", queryCondition.getPartner().getName());
        params.put("legalperson", queryCondition.getPartner().getLegalPerson());
        params.put("targetstart", queryCondition.getTargetstart());
        params.put("targetend", queryCondition.getTargetend());
        params.put("quality_marginstart", queryCondition.getQuality_marginstart());
        params.put("quality_marginend", queryCondition.getQuality_marginend());
        params.put("penaltystart", queryCondition.getPenaltystart());
        params.put("penaltyend", queryCondition.getPenaltyend());
        params.put("other", queryCondition.getOther());
        params.put("pay", queryCondition.getPay());
        params.put("warrantystart", queryCondition.getWarrantystart());
        params.put("warrantyend", queryCondition.getWarrantyend());
        params.put("payPlanDateStart", queryCondition.getDate1_start());
        params.put("payPlanDateEnd", queryCondition.getDate1_end());
        params.put("isEffect", queryCondition.getIsEffect());
        return params;
    }

    public DataModel<MainInformation> getContractModel() {
        return contractModel;
    }

    public void setContractModel(DataModel<MainInformation> contractModel) {
        this.contractModel = contractModel;
    }

    public DocumentQueryConditionBeanContract getQueryCondition() {
        return queryCondition;
    }

    public void setQueryCondition(DocumentQueryConditionBeanContract queryCondition) {
        this.queryCondition = queryCondition;
    }

    public boolean isQuerybut() {
        return querybut;
    }

    public void setQuerybut(boolean querybut) {
        this.querybut = querybut;
    }
}
